<template>
  <div class="container">
    <el-dialog
      :title="` ${data.id ? '修改' : '新增'} `"
      :visible.sync="dialogFormVisible"
      width="400px"
      @open="openWindow()"
    >
      <el-form :model="info" :rules="formRules" ref="form">
        <el-form-item prop="subjectID" label="所属学科" label-width="80px">
          <el-select
            v-model="info.subjectID"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in allInfo"
              :value="item.value"
              :label="item.label"
              :key="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="directoryName" label="目录名称" label-width="80px">
          <el-input
            v-model="info.directoryName"
            style="width: 100%"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
import { add, update } from '@/api/hmmm/directorys'
export default {
  props: {
    data: {
      default: () => {},
      type: Object
    }
  },
  data () {
    return {
      dialogFormVisible: false,
      info: {
        id: null,
        subjectID: null,
        directoryName: null
      },
      allInfo: [],
      formRules: {
        subjectID: [
          { type: 'number', message: '请选择所属学科', tirgger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', tirgger: 'blur' }
        ]
      }
    }
  },
  async created () {
    const { data: label } = await simple()
    this.allInfo = label
  },
  methods: {
    openWindow () {
      if (this.data.id) {
        this.info = this.data
        // this.info.subjectID = this.data.subjectName;
        // this.info.directoryName = this.data.directoryName;
      }
    },
    open () {
      this.dialogFormVisible = true
    },
    // 添加
    confirm () {
      this.$refs.form.validate(async (flag) => {
        if (flag) {
          // 提交
          if (!this.data.id) {
            // 新增
            await add(this.info)
            this.$message.success('添加成功')
          } else {
            // 修改
            this.info.id = this.data.id
            await update(this.info)
            this.$message.success('编辑成功')
          }
          this.dialogFormVisible = false
          this.$emit('updateList')
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
